package cn.stylefeng.guns.modular.storeProcurement.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;

import cn.stylefeng.guns.core.util.DateUtil;
import cn.stylefeng.guns.modular.basic.entity.BasicCustomer;
import cn.stylefeng.guns.modular.basic.entity.BasicPutgoods;
import cn.stylefeng.guns.modular.basic.entity.BasicWarehouse;
import cn.stylefeng.guns.modular.basic.model.params.BasicPutgoodsParam;
import cn.stylefeng.guns.modular.basic.model.result.BasicPutgoodsResult;
import cn.stylefeng.guns.modular.basic.service.BasicCustomerService;
import cn.stylefeng.guns.modular.basic.service.BasicPutgoodsService;
import cn.stylefeng.guns.modular.basic.service.BasicWarehouseService;
import cn.stylefeng.guns.modular.storeProcurement.entity.StoreProcurement;
import cn.stylefeng.guns.modular.storeProcurement.model.params.StoreProcurementParam;
import cn.stylefeng.guns.modular.storeProcurement.service.StoreProcurementService;
import cn.stylefeng.guns.sys.modular.system.entity.Dept;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.DeptService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 * 门店采购单控制器
 *
 * @author wp
 * @Date 2025-09-02 10:19:26
 */
@Controller
@RequestMapping("/storeProcurement")
public class StoreProcurementController extends BaseController {

    private String PREFIX = "/storeProcurement";

    @Autowired
    private StoreProcurementService storeProcurementService;

    @Autowired
    private UserService userService;

    @Autowired
    private BasicCustomerService basicCustomerService;

    @Autowired
    private BasicWarehouseService basicWarehouseService;

    @Autowired
    private BasicPutgoodsService basicPutgoodsService;

    @Autowired
    private DeptService deptService;

    /**
     * 跳转到主页面
     *
     * @author wp
     * @Date 2025-09-02
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/storeProcurement.html";
    }

    /**
     * 新增页面
     *
     * @author wp
     * @Date 2025-09-02
     */
    @RequestMapping("/add")
    public String add(Model model) {

        User user = userService.getcurrentUser();
        Long deptId = user.getDeptId();
        Long deliverId = user.getDeliverId();
        //查询可选择仓库
        model.addAttribute("basicWarehouseList", basicWarehouseService.findAvailableList(deptId,deliverId,user.getOperateId()));
        //查询可选择客户
        model.addAttribute("customerlist", basicCustomerService.findAvailableList(deptId,deliverId,user.getOperateId()));

        BasicPutgoodsParam basicPutgoodsParam = new BasicPutgoodsParam();
        basicPutgoodsParam.setUnitCategory("门店采购");
        basicPutgoodsParam.setUnitType("门店");
        basicPutgoodsParam.setDeptId(deptId);
        List<BasicPutgoodsResult> pglist = basicPutgoodsService.findListBySpec(basicPutgoodsParam,user.getOperateId());
        model.addAttribute("pglist", pglist);

        if(ToolUtil.isEmpty(basicPutgoodsParam.getUnitType())) {
            basicPutgoodsParam.setUnitType("门店");
        }
        basicPutgoodsParam.setDeliverId(user.getDeliverId());
        LayuiPageInfo pageBySpecHx = this.basicPutgoodsService.findPageBySpecHx(basicPutgoodsParam, null, deptId, user.getOperateId());
        model.addAttribute("basicUnitList", pageBySpecHx.getData());

        return PREFIX + "/storeProcurement_add.html";
    }

    /**
     * 编辑页面
     *
     * @author wp
     * @Date 2025-09-02
     */
    @RequestMapping("/edit")
    public String edit(Model model) {
        User user = userService.getcurrentUser();
        Long deptId = user.getDeptId();
        Long deliverId = user.getDeliverId();
        //查询可选择仓库
        model.addAttribute("basicWarehouseList", basicWarehouseService.findAvailableList(deptId,deliverId,user.getOperateId()));
        //查询可选择客户
        model.addAttribute("customerlist", basicCustomerService.findAvailableList(deptId,deliverId,user.getOperateId()));

        BasicPutgoodsParam basicPutgoodsParam = new BasicPutgoodsParam();
        basicPutgoodsParam.setUnitCategory("门店采购");
        basicPutgoodsParam.setUnitType("门店");
        basicPutgoodsParam.setDeptId(deptId);
        List<BasicPutgoodsResult> pglist = basicPutgoodsService.findListBySpec(basicPutgoodsParam,user.getOperateId());
        model.addAttribute("pglist", pglist);

        if(ToolUtil.isEmpty(basicPutgoodsParam.getUnitType())) {
            basicPutgoodsParam.setUnitType("门店");
        }
        basicPutgoodsParam.setDeliverId(user.getDeliverId());
        LayuiPageInfo pageBySpecHx = this.basicPutgoodsService.findPageBySpecHx(basicPutgoodsParam, null, deptId, user.getOperateId());
        model.addAttribute("basicUnitList", pageBySpecHx.getData());
        return PREFIX + "/storeProcurement_edit.html";
    }

    /**
     * 新增接口
     *
     * @author wp
     * @Date 2025-09-02
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(StoreProcurementParam storeProcurementParam) {


        User user = userService.getcurrentUser();
        String trayCode = getOperateIdOrderPrefix(user);
        if(ToolUtil.isNotEmpty(storeProcurementParam.getOrderOutNumber())) {
            //校验客户单号 是否存在
            if (this.storeProcurementService.checkCustomerOrder(storeProcurementParam.getOrderOutNumber())) {
                return ResponseData.error("单号已存在");
            }
        } else {
            storeProcurementParam.setOrderOutNumber(trayCode+"CK"+ DateUtil.getDatesBch()+DateUtil.getRandomNumber());
        }
        Object WarehouseId = storeProcurementParam.getSetWarehouseId();
        if (WarehouseId!=null) {
            BasicWarehouse bawBasicWarehouse = basicWarehouseService.getById(Integer.parseInt(WarehouseId.toString()));
            storeProcurementParam.setSetWarehouse(bawBasicWarehouse.getWarehouseName());//set 仓库名称
        }
        Object CustomerId = storeProcurementParam.getCustomerId();
        if (CustomerId!=null) {
            BasicCustomer basicCustomer = basicCustomerService.getById(Integer.parseInt(CustomerId.toString()));
            storeProcurementParam.setCustomerName(basicCustomer.getCustomerName());//set 客户名称
        }
        Object setUnitId = storeProcurementParam.getGetUnitId();
        if (setUnitId!=null) {
            BasicPutgoods basicPutgoods = basicPutgoodsService.getById(Integer.parseInt(setUnitId.toString()));
            storeProcurementParam.setGetUnit(basicPutgoods.getPutgoodsName());
        }
        storeProcurementParam.setDeptId(user.getDeptId());
        storeProcurementParam.setDeliverId(user.getDeliverId());
        storeProcurementParam.setOrderState("待提交");//set 单据状态
        storeProcurementParam.setCreateUserName(user.getName());//set 制单用户名称
        this.storeProcurementService.add(storeProcurementParam);


        return ResponseData.success();
    }
    /**
     * 获取所属项目单号前缀
     *
     * @param user
     * @return
     */
    public String getOperateIdOrderPrefix(User user) {
        if (user.getOperateId() != null) {
            Dept dept = new Dept();
            dept.setOperateId(user.getOperateId());
            Dept resdept = deptService.findBySpec(dept);
            if (resdept != null) {
                if (ToolUtil.isNotEmpty(resdept.getOrderPrefix())) {
                    return resdept.getOrderPrefix();
                } else {
                    return user.getAccount().substring(0, 3);
                }
            } else {
                return null;
            }
        } else {
            return null;
        }
    }
    /**
     * 编辑接口
     *
     * @author wp
     * @Date 2025-09-02
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(StoreProcurementParam storeProcurementParam) {
        Object WarehouseId = storeProcurementParam.getSetWarehouseId();
        if (WarehouseId!=null) {
            BasicWarehouse bawBasicWarehouse = basicWarehouseService.getById(Integer.parseInt(WarehouseId.toString()));
            storeProcurementParam.setSetWarehouse(bawBasicWarehouse.getWarehouseName());//set 仓库名称
        }
        this.storeProcurementService.update(storeProcurementParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author wp
     * @Date 2025-09-02
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(StoreProcurementParam storeProcurementParam) {
        this.storeProcurementService.delete(storeProcurementParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author wp
     * @Date 2025-09-02
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(StoreProcurementParam storeProcurementParam) {
        StoreProcurement detail = this.storeProcurementService.getById(storeProcurementParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author wp
     * @Date 2025-09-02
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(StoreProcurementParam storeProcurementParam,String condition ) {
        return this.storeProcurementService.findPageBySpec(storeProcurementParam,condition);
    }

}


